//
//  DataViewController.swift
//  BusinessIOS
//
//  Created by Rain on 2018/11/2.
//  Copyright © 2018 XingJian Li. All rights reserved.
//

import UIKit
import Alamofire
class DataViewController: UIViewController {
    var tableView: UITableView!
    
    var  numstring:String!
    
      var DataArray = [DataModel]()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = bgGrayColor;
        
        navigationItem.title="资料下载"
        
        tableView = UITableView(frame:self.view.frame, style:.plain)
        tableView.dataSource = self
        tableView.delegate = self
        tableView.showsHorizontalScrollIndicator = false
        tableView.showsVerticalScrollIndicator = false
        tableView.separatorStyle = .none
        //tableView.rowHeight=50
        //tableView.separatorInset = UIEdgeInsets.zero
        //tableView.contentInset = UIEdgeInsetsMake(-20, 0, 0, 0)//去除表格上放多余的空隙
        //tableView.tableHeaderView = UIView.init()
        tableView.tableFooterView = UIView.init()
        
        
        tableView.register(NSClassFromString("DataTableViewCell"), forCellReuseIdentifier: "DataTableViewCell")
        
        
        view.addSubview(tableView)
        tableView.sd_layout()?.spaceToSuperView(UIEdgeInsets.zero)
        
        self.loadData()
        
    }
    func loadData(){
        let urlString = "http://wwwqjzsyzcscom.aykj.net.cn/api/article/getArticleList"
        
        let json = "{\"newsClassId\":\"\(numstring!)\",\"start\":\"\(1)\",\"pageSize\":\"\(5)\"}"
        
        //             let json = "{\"account\":\"\(phoneTextField.text!)\",\"password\":\"\(PasswordTextField.text!)\",\"validateCode\":\"\(verificationTextField.text!)\"}"
        print("json====\(json)")
        
        let url = URL(string: urlString)!
        let jsonData = json.data(using: .utf8, allowLossyConversion: false)!
        
        var request = URLRequest(url: url)
        request.httpMethod = HTTPMethod.post.rawValue
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        request.httpBody = jsonData
        
        Alamofire.request(request).responseJSON {(response) in
            
            print(response)
            if response.result.isSuccess {
                let json = JSON(response.data!)
                let array = json["resultData"].array
                
                for dataDic in  array! {
                    
                    let model1 =   DataModel()
                    
                    
                    model1.articleAttachmentName = dataDic["articleAttachmentName"].string
                    
                    model1.articleAttachmentPath = dataDic["articleAttachmentPath"].string
                    
                    model1.articleDetail = dataDic["articleDetail"].string
                    
                    model1.id = dataDic["id"].string
                    self.DataArray.append(model1)
                    
                    
                    print("DynamicArray====\(self.DataArray.count)")
                    
                }
                
                self.tableView.reloadData()
                
                
            }else{
                
            }
            
        }
    }



}
extension DataViewController: UITableViewDelegate, UITableViewDataSource {
    
    
    
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.DataArray.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return 60
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let ddataModel:DataModel = self.DataArray[indexPath.row]
        let cell = DataTableViewCell(style:UITableViewCellStyle.default, reuseIdentifier: "DataTableViewCell")
        cell.nameLabel.text = ddataModel.articleAttachmentName
        return cell
    }
    
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        
         let ddataModel:DataModel = self.DataArray[indexPath.row]
        
        
        let WebVC =  WebViewController()
        WebVC.numInteger = ddataModel.id
        self.navigationController?.pushViewController(WebVC , animated: true)
        
    }
    
}
